import { RedisCommonService } from '@/common/redis/redis.service';
import { MiddlewareConsumer, Module } from '@nestjs/common';
import { UsersModule } from '../users/users.module';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import {
  CaptchaMiddleware,
  visCaptcha,
} from './middleware/captcha/captcha.middleware';
import { PassportModule } from '@nestjs/passport';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ConfigEnum } from '@/common/enum/config.enum';
import { JwtStrategy } from './jwt.strategy';
import { JwtModule } from '@nestjs/jwt';
import * as multer from 'multer';

@Module({
  imports: [
    UsersModule,
    PassportModule, // 中间件
    JwtModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          // global: true,
          secret: configService.get<string>(ConfigEnum.JWT_SECRET),
          signOptions: {
            expiresIn: Number(
              configService.get<string>(ConfigEnum.JWT_EXPIRES_IN),
            ),
          },
        };
      },
    }),
  ],
  controllers: [AuthController],
  providers: [AuthService, RedisCommonService, JwtStrategy],
})
export class AuthModule {
  // 控制器级别使用中间件
  configure(consumer: MiddlewareConsumer) {
    // consumer.apply(CaptchaMiddleware).forRoutes(AuthController); // 指定控制器或一组控制器
    // consumer.apply(CaptchaMiddleware).forRoutes({ path: 'auth/captcha', method: RequestMethod.GET }); // 指定路由和请求方法
    consumer.apply(CaptchaMiddleware).forRoutes('auth/captcha'); // 指定路由
    // consumer.apply(multer().none(), visCaptcha).forRoutes('auth/login111');
    consumer.apply(multer().none(), visCaptcha).forRoutes('auth/login');
  }
}
